package com.zking.utils.廖全箐;

import com.zking.dao.FoodDao;
import com.zking.entity.Food;

import java.sql.*;
import java.util.Vector;

public class JDBC {
    private Statement sta = null;
    private Connection conn = null;
    private PreparedStatement psta = null;
    private ResultSet rs = null;
    /*
        数据库连接
     */
    public Connection getConnection() {

        //加载驱动
        //得到这个类的反射
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动程序

            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/rz1217";
            String user = "root";
            String password = "root";
             conn = DriverManager.getConnection(url,user,password);
            //测试连接是否成功
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    //获取全部数据
    public void selectTable(Vector<Food>fod){
        String sql = "select * from food";
        try {
            sta = conn.createStatement();//利用连接器创建语句
            rs  = sta.executeQuery(sql);
            //将语句发送到数据库,返回数据，rs相当于游标指针，可用next()方法访问下一条数据，若有数据返回ture,若无数据返回false
            while (rs.next()){
                Food food = new Food();
                String name = rs.getString("Name");
                int price = rs.getInt("Price");
                String shop = rs.getString("Shop");
                String sort = rs.getString("sort");
                //储存对象
                food.setName(name);
                food.setPrice(price);
                food.setShop(shop);
                food.setSort(sort);
                fod.add(food);
                System.out.println("商店名称:"+shop+"价格"+price+"菜品"+shop+"分类"+sort);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
        //信息检索
        int count = 0;
    public  int selectTable(String sql,Vector<Object>vct){
        Vector<Food>fod_back = new Vector<Food>();
        try {
            psta = conn.prepareStatement(sql);
            if(vct != null){
                for(int i = 0;i<vct.size();i++){
                    psta.setObject(i+1,vct.get(i));
                }
            }
            rs = psta.executeQuery();
            while (rs.next()){
                Food food = new Food();
                String name = rs.getString("Name");
                int price = rs.getInt("Price");
                String shop = rs.getString("Shop");
                String sort = rs.getString("sort");
                //储存对象
                food.setName(name);
                food.setPrice(price);
                food.setShop(shop);
                food.setSort(sort);
                fod_back.add(food);
                System.out.println("商店名称:"+shop+"价格"+price+"菜品"+shop+"分类"+sort);
                count++;
            }
          // 搜寻结果，显示结果窗口
            if(count>=1){
              //  ShowResult show = new ShowResult();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
        public void MyClose(){
            try {
                if(conn!=null){
                   conn.close();
                }
                if(psta!=null){
                    psta.close();
                }
                if(rs!=null){
                    rs.close();
                }
            }catch (Exception e){

            }
        }

}
